<?php

/**
 * Louder Cache
 *
 * LICENSE
 *
 * This source file is subject to the New BSD License that is bundled
 * with this package in the file docs/LICENSE.txt.
 *
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to info@loudertechnology.com so we can send you a copy immediately.
 *
 * @category Louder
 * @package CacheServer
 * @copyright Copyright (c) 2005-2009 Andres Felipe Gutierrez (gutierrezandresfelipe at gmail.com)
 * @license New BSD License
 */

/**
 * Clase que administra el servidor de cacheo
 *
 * @category Louder
 * @package CacheServer
 * @copyright Copyright (c) 2005-2009 Andres Felipe Gutierrez (gutierrezandresfelipe at gmail.com)
 * @license New BSD License
 */
class CacheServer {

	/**
	 * URI para servicios del servidor de cacheo
	 *
	 * @var string
	 */
	private static $_serviceURI = "http://louder-cache-service";

	/**
	 * Inicia una transaccion
	 *
	 * @param string $username
	 * @param string $password
	 * @return boolean
	 */
	public function startTransaction($username, $password){
		if($username=="admin"&&$password=="admin"){
			return $this->_returnValue(true);
		} else {
			return $this->_returnValue(false);
		}
	}

	/**
	 * Devuelve el nodo raiz del arbol
	 *
	 * @access public
	 * @return string
	 */
	public function getRootNode(){
		CacheStorage::initStorage();
		return $this->_returnValue(CacheStorage::getRootNode());
	}

	/**
	 * Agrega un atributo a un nodo
	 *
	 * @param int $id
	 * @param string $index
	 * @param mixed $value
	 * @return boolean
	 */
	public function addNodeAttribute($id, $index, $value){
		CacheStorage::initStorage();
		return $this->_returnValue(CacheStorage::addNodeAttribute($id, $index, $value));
	}

	/**
	 * Obtiene el valor de un atributo
	 *
	 * @param int $id
	 * @param string $index
	 * @param mixed $value
	 * @return mixed
	 */
	public function getNodeAttribute($id, $index){
		CacheStorage::initStorage();
		return $this->_returnValue(CacheStorage::getNodeAttribute($id, $index));
	}

	/**
	 * Agrega un nodo hijo a otro nodo
	 *
	 * @access public
	 * @return string
	 */
	public function addChild($path, $items){
		CacheStorage::initStorage();
		return $this->_returnValue(CacheStorage::addChild($path, $items));
	}

	/**
	 * Raliza una consulta sobre el arbol y devuelve los resultados
	 *
	 * @access public
	 * @param string $path
	 * @return string
	 */
	public function query($path){
		CacheStorage::initStorage();
		return $this->_returnValue(CacheStorage::query($path));
	}

	function gt(){
		CacheStorage::initStorage();
		return $this->_returnValue(CacheStorage::gt());
	}

	/**
	 * Crea un valor serializado y en base64 para evitar que se pierdan datos al transferirlo
	 *
	 * @param mixed $value
	 * @return string
	 */
	private function _returnValue($value){
		return base64_encode(serialize($value));
	}

	/**
	 * Atiende las peticiones realizadas al servidor de cacheo
	 *
	 * @access public
	 * @static
	 */
	public static function handleRequests(){
		try {
			if(extension_loaded("soap")==false){
				throw new CacheServerException("La extensión php_soap es requerida para usar el CacheServer");
			}
			$config = new ConfigReader("config-server.xml");
			$serverId = $config->getAttributeFromNode("server-info", "id");
			session_id($serverId);
			$soapServer = new SoapServer(null, array('uri' => self::$_serviceURI));
			$soapServer->setClass("CacheServer");
			$soapServer->setPersistence(SOAP_PERSISTENCE_REQUEST);
			$soapServer->handle();
		}
		catch(Exception $e){
			$soapServer->fault("0", $e->getMessage());
		}
	}

}